package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseApiQuery;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.service.edu.entity.vo.CourseInfoVo;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-06-24
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CourseDescriptionMapper courseDescriptionMapper;

    // 保存课程信息
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfo) {

        // 向course表中保存信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfo, course);
        courseMapper.insert(course);
        String courseId = course.getId();

        // 向CourseDescription中保存课程简介信息
        // 因为需要使用course表中的id, 不能使用雪花算法自动生成id
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfo.getDescription());
        courseDescription.setId(courseId);
        courseDescriptionMapper.insert(courseDescription);

        return courseId;
    }

    // 查询课程信息
    @Override
    public CourseInfoForm getCourseInfo(String courseId) {

        CourseInfoForm courseInfoForm = new CourseInfoForm();

        Course course = courseMapper.selectById(courseId);
        BeanUtils.copyProperties(course, courseInfoForm);

        CourseDescription courseDescription = courseDescriptionMapper.selectById(courseId);
        courseInfoForm.setDescription(courseDescription.getDescription());

        return courseInfoForm;
    }

    // 查询课程信息
    @Override
    public Page<CourseInfoVo> getCourseInfoVos(Integer pageNum, Integer pageSize) {

        Page<CourseInfoVo> page = new Page<>(pageNum, pageSize);

        QueryWrapper<CourseInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("1", "1");

        List<CourseInfoVo> courseInfoVos = courseMapper.getCourseInfoVos(page, queryWrapper);

        // 将查询出的分页数据集合复制给page对象
        page.setRecords(courseInfoVos);
        return page;
    }

    // 更新课程信息
    @Override
    public void updateCourse(String id, CourseInfoForm courseInfo) {
        // 将courseInfo中的部分信息复制到course中, 更新
        Course course = new Course();
        BeanUtils.copyProperties(courseInfo, course);
        course.setId(id);
        courseMapper.updateById(course);

        // 将courseInfo中的课程简介复制给CourseDescription, 更新
        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfo, courseDescription);
        courseDescription.setId(id);
        courseDescriptionMapper.updateById(courseDescription);
    }

    // 查询讲师下的所有课程
    @Override
    public List<Course> getCourseByTeacherId(String teacherId) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacherId);
        queryWrapper.eq("status", "Normal");
        queryWrapper.select("id", "title", "cover");
        queryWrapper.orderByAsc("publish_time");
        List<Course> courses = courseMapper.selectList(queryWrapper);
        return courses;
    }

    // 带条件查询的课程列表
    @Override
    public List<Course> getCourseList(CourseApiQuery courseApiQuery) {

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String subjectParentId = courseApiQuery.getSubjectParentId();
        String subjectId = courseApiQuery.getSubjectId();
        String orderByColumn = courseApiQuery.getOrderByColumn();
        Integer type = courseApiQuery.getType();

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(orderByColumn)) {
            if (type==null || type==1) {
                queryWrapper.orderByDesc(orderByColumn);
            } else {
                queryWrapper.orderByAsc(orderByColumn);
            }
        }

        return courseMapper.selectList(queryWrapper);
    }

    // 查询课程详细信息 + 讲师信息 getCourseDetails
    @Override
    public CourseDetailsVo getCourseDetails(String courseId) {
        CourseDetailsVo courseDetails = courseMapper.getCourseDetails(courseId);
        courseDetails.setViewCount(courseDetails.getViewCount()+1);

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", courseId);
        updateWrapper.set("view_count", courseDetails.getViewCount());

        this.update(updateWrapper);

        return courseDetails;
    }


}
